/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package co.com.poligran.polla.mundialista.ingesoft.impl;

import co.com.poligran.polla.mundialista.ingesoft.base.Mesa;
import co.com.poligran.polla.mundialista.ingesoft.dao.MesaDAO;
import co.com.poligran.polla.mundialista.ingesoft.db.Entitie;
import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;

/**
 *
 * @author alejo
 */
public class MesaImpl extends Entitie implements MesaDAO {
 /*public static void main(String[] args) {
        MesaDAO dAO = new MesaImpl();
        Mesa u = new Mesa(1,new BigDecimal(25000),0,"1",2,2);
        //System.out.println(dAO.insert(u));
        //for (Mesa un : dAO.selectAll()) {
          //  System.out.println(un);
        //}
        //System.out.println("Login: " + dAO.Login("gg@elearn.edu.mx", "1234"));
        //System.out.println("Existe: " + dAO.select(4));
        //System.out.println("Delete: " + dAO.delete(2));
       System.out.println("Update: " + dAO.update(u));

   }*/
    
    
    
    @Override
    public boolean insert(Mesa e) {
      boolean inserto = false;
        try {
            this.cs = conn.prepareCall("EXEC sp_InsertMesa ?,?,?,?,?");

            this.cs.setBigDecimal(1, e.getMonto());
            this.cs.setInt(2, e.getCuposDisponibles());
            this.cs.setString(3, e.getEstado());
            this.cs.setInt(4, e.getIdSala());
            this.cs.setInt(5, e.getIdPartido());
     

            this.cs.executeUpdate();

            inserto = true;

        } catch (SQLException ex) {
            System.out.print("exepction  " + ex.getMessage());
        } finally {
            if (this.cs != null) {
                try {
                    this.cs.close();
                } catch (SQLException ex) {
                    System.out.print("exepction  " + ex.getMessage());
                }
            }
        }
        return inserto; 
    }

    @Override
    public Mesa select(int idEntitie) {
       Mesa selectMesa = null;
        try {
            this.query = "EXEC sp_GetMesaById ?";
            this.cs = conn.prepareCall(query, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            this.cs.setInt(1, idEntitie);
            this.rs = cs.executeQuery();
            if (rs.next()) {
                selectMesa = new Mesa();
                selectMesa.setIdMesa(rs.getInt(1));
                selectMesa.setMonto(rs.getBigDecimal(2));
                selectMesa.setCuposDisponibles(rs.getInt(3));
                selectMesa.setEstado(rs.getString(4));
                selectMesa.setIdSala(rs.getInt(5));
                selectMesa.setIdPartido(rs.getInt(6));
            }
        } catch (SQLException sqle) {
            System.out.println(sqle.getMessage());
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (ps != null) {
                    ps.close();
                }
            } catch (SQLException sqlers) {
                System.out.println(sqlers.getMessage());
            }
        }
        return selectMesa;
    }

    @Override
    public LinkedList<Mesa> selectAll() {
        LinkedList<Mesa> selectMesas = null;
        try {
            this.query = "select * from mesa";
            this.ps = conn.prepareStatement(query, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            this.rs = ps.executeQuery();
            selectMesas = new LinkedList();
            while (rs.next()) {
                Mesa mesa = new Mesa();
                mesa.setIdMesa(rs.getInt(1));
                mesa.setMonto(rs.getBigDecimal(2));
                mesa.setCuposDisponibles(rs.getInt(3));
                mesa.setEstado(rs.getString(4));
                mesa.setIdSala(rs.getInt(5));
                mesa.setIdPartido(rs.getInt(6));
                
                selectMesas.add(mesa);
            }
        } catch (SQLException sqle) {
            System.out.println(sqle.getMessage());
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (ps != null) {
                    ps.close();
                }
            } catch (SQLException sqlers) {
                System.out.println(sqlers.getMessage());
            }
        }
        return selectMesas;
    }

    @Override
    public boolean update(Mesa e) {
         boolean update = false;
        try {
            this.cs = conn.prepareCall("sp_UpdateMesa ?,?,?,?,?,?");

            this.cs.setInt(1, e.getIdMesa());
            this.cs.setBigDecimal(2, e.getMonto());
            this.cs.setInt(3, e.getCuposDisponibles());
            this.cs.setString(4, e.getEstado());
            this.cs.setInt(5, e.getIdSala());
            this.cs.setInt(6, e.getIdPartido());

            // call executeUpdate
            if (this.cs.executeUpdate() == 1) {
                update = true;
            }

        } catch (SQLException ex) {
            System.out.print("exepction  " + ex.getMessage());
        } finally {
            if (this.cs != null) {
                try {
                    this.cs.close();
                } catch (SQLException ex) {
                    System.out.print("exepction  " + ex.getMessage());
                }
            }
        }
        return update;
    }

    @Override
    public boolean delete(int idEntitie) {
        boolean resp = false;
        try {
            this.query = "EXEC sp_DeleteMesa ?";
            this.cs = conn.prepareCall(query, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            // set preparedstatement 
            this.cs.setInt(1, idEntitie);
            // call executeUpdate
            if (this.cs.executeUpdate() == 1) {
                resp = true;
            }
        } catch (SQLException se) {
            System.out.print("exepction  " + se.getMessage());
        } finally {
            try {
                this.cs.close();
            } catch (SQLException ex) {
                System.out.print("exepction  " + ex.getMessage());
            }
        }
        return resp;
    }
    
    
}
